<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="UTF-8"%>
<%@page import="com.king.util.date.DateUtil"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="com.rong.common.util.ImageUtils"%>
<%

   response.setContentType("text/html");
//   图片上传路径
   //String uploadPath =request.getSession().getServletContext().getRealPath("/")+"images/code";
	String uploadPath ="/data/images/wxProCode/";
//   图片临时上传路径
   //String tempPath = request.getSession().getServletContext().getRealPath("/")+"images/code/temp/";
String tempPath = "/data/images/wxProCode/temp/";
//   图片网络相对路径
   String imagePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/"; 
//   文件夹不存在就自动创建：
   if(!new File(uploadPath).isDirectory())
   new File(uploadPath).mkdirs();
   if(!new File(tempPath).isDirectory())
   new File(tempPath).mkdirs();
   try {
	   DiskFileUpload fu = new DiskFileUpload();
	//   设置最大文件尺寸，这里是4MB
	   fu.setSizeMax(4194304);
	//   设置缓冲区大小，这里是4kb
	   fu.setSizeThreshold(4096);
	//   设置临时目录：
	   fu.setRepositoryPath(tempPath);
	//   得到所有的文件：
	   List fileItems = fu.parseRequest(request);
	   Iterator i = fileItems.iterator();
	//   依次处理每一个文件：
	   while(i.hasNext()) 
	   {
		   FileItem file = (FileItem)i.next();
		//   获得文件名，这个文件名是用户上传时用户的绝对路径：
		   String sourcefileName = file.getName();
		   if(sourcefileName!=null&&(sourcefileName.endsWith(".jpg")||sourcefileName.endsWith(".gif")||sourcefileName.endsWith(".png"))) 
		   {
				//   在这里可以记录用户和文件信息,生成上传后的文件名
				   String destinationfileName="";
				
				   if(sourcefileName.endsWith(".jpg"))
				   {
				   		destinationfileName= DateUtil.getDateString("yyyyMMddHHmmss") + ".jpg";
				   }
				   else if(sourcefileName.endsWith(".gif"))
				   {
				   		destinationfileName= DateUtil.getDateString("yyyyMMddHHmmss")  + ".gif";
				   }
				   else if(sourcefileName.endsWith(".png"))
				   {
				   		destinationfileName= DateUtil.getDateString("yyyyMMddHHmmss") + ".png";
				   }
				   File f1=new File(uploadPath+ "/" + destinationfileName);
				   file.write(f1);
					String imgUrl=uploadPath + destinationfileName;
					String templateUrl =request.getSession().getServletContext().getRealPath("/")+"images/template1.jpg";
					String type  =request.getParameter("type");
					if("code".equals(type)){
					    ImageUtils.pressImage(imgUrl, templateUrl, 0, 0);
				  	 }
					
				   //out.print("{\"result\":0,\"message\":\"上传成功\",\"imageUrl\":\""+imagePath+"images/code/"+ destinationfileName +"\"}");
				   out.print("{\"imgUrl\":\""+imgUrl+"\",\"templateUrl\":\""+templateUrl+"\",\"result\":0,\"message\":\"上传成功\",\"imageUrl\":\"http://images.kuaiyoukuaidi.com/wxProCode/"+ destinationfileName +"\"}");
				   //out.print("{\"imgUrl\":\""+imgUrl+"\",\"templateUrl\":\""+templateUrl+"\",\"result\":0,\"message\":\"上传成功\",\"imageUrl\":\""+imagePath+"images/code/"+ destinationfileName +"\"}");
				   
				   
				   //out.print("<img src="+imagePath+"upload/images/"+destinationfileName+">");
					
		   }
		   else
		   {
			   out.print("{\"result\":0,\"message\":\"上传文件出错，只能上传 *.jpg , *.gif,*.png\"}");
			   return;
		   }
	   }
   }
   catch(Exception e) 
   {
	 e.printStackTrace();
	 out.print("{\"result\":-99,\"message\":\"上传失败，系统异常\"}");
   }
   out.flush();
   out.close();
%>
